< previous page page_697 next page >

Page 697
   Sub DisplayAmount(nAmount)
      Dim sHTML

      sHTML = "<FONT "
      If nAmount < 2000 Then sHTML = sHTML & "COLOR=RED"
      If nAmount > 4000 Then sHTML = sHTML & "COLOR=GREEN"
      sHTML = sHTML & ">"

      sHTML = sHTML & FormatCurrency(nAmount)
      sHTML = sHTML & "</FONT>"
      Response.Write sHTML
   End Sub
The DisplayAmount subroutine accepts a numeric value and then colors and formats it based on the value itself. Procedures like this one would be very useful in many ASP files, but there is no way to declare this subroutine as public to all ASP files. You could cut and paste it into each file, but maintaining all the copies of it would become a chore. The answer to this problem is to move the function to a separate file and include it where necessary. To include other files in the current ASP file, use the #INCLUDE statement. Two example files are as follows:
<!#INCLUDE VIRTUAL = "/test/formatfuncs.asp">

<! #INCLUDE FILE="C:\Inetpub\wwwroot\test\formatfuncs.asp" >
The first line of code uses the virtual directory to specify an include file. The next statement uses the physical directory. Either way, the end result is that the contents of formatfuncs.asp are inserted into the current ASP file before script execution. Some common uses of the #INCLUDE directive are to add footers at the bottom of each Web page, such as the company name or e-mail address.
Database Access with Active Server Pages
Suppose you want to publish the contents of a database on the Web. With ASP, you can use ADO objects in your VBScript code to access databases. As discussed in Chapter 28, ''Using ActiveX Data Objects (ADO)," to access a database you first need to identify a data source. The actual database can be located on the Web server itself or on another machine, as long as you can connect to it via a data source.
When setting up data sources for use with ASP, you generally should use a System Data Source Name (DSN) rather than a User DSN. The System DSN allows the data source to be available to the Web server at all times, not just when a certain user is logged in.
sarrow.gifSee "Setting Up a Data Source," p. 613
note.gif You can also use a DSN-less connection that does not require you to set up a data source. This is also discussed in Chapter 28. square.gif

In this section, the examples assume that the BIBLIO data source has been set up as described in Chapter 26, "Using Data Access Objects (DAO)."
sarrow.gifSee "Setting Up a DAO Project," p. 567

 
< previous page page_697 next page >